package org.jeecg.modules.flow.callback.delivery.move;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.transfer.vo.WmsMoveReceiptPage;
import org.jeecg.modules.dock.entity.DockTaskRecordMap;
import org.jeecg.modules.dock.service.IDockTaskRecordMapService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 条料调拨因入移库单 备料
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-11-27
 */
@RequiredArgsConstructor
@Component
public class Y103MoveReceiptSaveBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsMoveReceiptService wmsMoveReceiptService;

    private final IWmsRepertoryService wmsRepertoryService;

    private final IDockTaskRecordMapService dockTaskRecordMapService;

    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        //根据提交数据 创建移库单
        baseFlowDto.translateClass(WmsMoveReceiptPage.class);
        WmsMoveReceiptPage wmsMoveReceiptPage = (WmsMoveReceiptPage) baseFlowDto.getData();
        WmsMoveReceipt wmsMoveReceipt = new WmsMoveReceipt();

        BeanUtils.copyProperties(wmsMoveReceiptPage, wmsMoveReceipt);
        String simpleUUID = IdUtil.simpleUUID();
        wmsMoveReceipt.setTaskCode(simpleUUID);
        String receiptId = wmsMoveReceiptService.saveMain(wmsMoveReceipt, wmsMoveReceiptPage.getWmsMoveReceiptMaterialList());
        //提交出库单
        wmsRepertoryService.moveSubmit(receiptId);
        wmsMoveReceiptService.update(new LambdaUpdateWrapper<WmsMoveReceipt>()
                .set(WmsMoveReceipt::getDocumentStatus, DocumentStatusEnum.COMMIT.getCode())
                .eq(WmsMoveReceipt::getId, receiptId)
        );
        //将taskCode和receiptId插入dockMap表
        DockTaskRecordMap dockTaskRecordMap=new DockTaskRecordMap();
        dockTaskRecordMap.setTaskId(receiptId);
        dockTaskRecordMap.setMesTaskCode(simpleUUID);
        dockTaskRecordMap.setRemark("移库单创建");
        dockTaskRecordMapService.save(dockTaskRecordMap);
        baseFlowDto.setInfoId(receiptId);
        baseFlowDto.setResult(Result.OK());
    }
}
